package com.xiaohei.rabbitmq.seven;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.xiaohei.rabbitmq.utils.RabbitmqUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Author: 王希辰
 * @Date: 2022/3/18 11:18
 * 声明主题交换机
 * 消费者1号
 */
public class ReceiveLogsTopic02 {
    public static final String EXCHANGE_NAME="topic_logs";
    public static final String QUEUE_NAME="Q2";

    public static void main(String[] args) throws Exception {
        Channel channel= RabbitmqUtils.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        //声明一个队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);

        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"lazy.#");

        System.out.println("等待接收消息...");

        //接收消息
        //接收消息
        DeliverCallback deliverCallback=(consumerTag, message)->{
            System.out.println("消息："+new String(message.getBody(), StandardCharsets.UTF_8)+
                    "=============接收队列："+QUEUE_NAME+"绑定键："+message.getEnvelope().getRoutingKey());
        };
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag -> {});
    }
}
